import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode, UploadFileParams } from '#/axios';
import { AjaxResult } from '@/api/model';
import { AxiosProgressEvent } from 'axios';
import { PreferenceEnum } from '@/enums/preferenceEnum';

/** 文件服务URL枚举 */
enum Api {
  /** 获取映射到静态资源的相对请求路径 */
  RequestPath = '/api/file/requestPath',
  /** 计算文件的Base64字符串 */
  CallFileBase64String = '/api/file/callFileBase64String',
  /** 缓存式文件上传 */
  Upload = '/api/file/upload',
  /** 流式文件上传 */
  UploadStream = '/api/file/uploadStream',
  /** 指定存储方式缓存式文件上传 */
  UploadByStoreMode = '/api/file/uploadByStoreMode',
  /** 指定存储方式流式文件上传 */
  UploadStreamByStoreMode = '/api/file/uploadStreamByStoreMode',
  /** 缓存式文件上传并复制一份至指定存储 */
  UploadAndCopy = '/api/file/uploadAndCopy',
  /** 流式文件上传并复制一份至指定存储 */
  UploadStreamAndCopy = '/api/file/uploadStreamAndCopy',
  /** 下载文件 */
  Download = '/api/file/download',
  /** 删除文件 */
  Delete = '/api/file/delete',
  /** 缓存式文件更新 */
  Update = '/api/file/update',
  /** 流式文件更新 */
  UpdateStream = '/api/file/updateStream',
  /** 以目标存储方式复制文件 */
  CopyToTarget = '/api/file/copyToTarget',
  /** 以目标存储方式迁移文件 */
  MoveToTarget = '/api/file/moveToTarget',
  /** 读取文件简介列表信息 */
  ReadIntro = '/api/file/readIntro',
  /** 根据上传标识获取文件简介 */
  GetIntroByUploadIdent = '/api/file/getIntroByUploadIdent',
  /** 根据上传标识删除文件 */
  DeleteByUploadIdent = '/api/file/deleteByUploadIdent',
  /** 读取文件存档信息 */
  ReadArchive = '/api/file/readArchive',
  /** 下载文件存档 */
  DownloadArchive = '/api/file/downloadArchive',
  /** 根据存档ID移除文件存档 */
  RemoveArchiveById = '/api/file/removeArchiveById',
  /** 根据简介ID移除文件存档 */
  RemoveArchiveByIntro = '/api/file/removeArchiveByIntro',
  /** 读取文件记录列表信息 */
  ReadRecord = '/api/file/readRecord',
  /** 获取LOGO */
  GetLogo = '/api/admin/logo/getLogo',
  /** 获取 LOGO Base64字符串 */
  GetLogoBase64String = '/api/admin/file/getLogoBase64String',
  /** LOGO上传 */
  UploadLogo = '/api/admin/file/uploadLogo',
  /** LOGO删除 */
  DeleteLogo = '/api/admin/file/deleteLogo',
}

/**
 * 获取映射到静态资源的相对请求路径
 * @param mode 错误消息模型
 */
export function requestPath(mode: ErrorMessageMode = 'none') {
  return httpClient.get<string>(
    {
      url: Api.RequestPath,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 计算文件的Base64字符串
 * @param params 多部分/表单数据：上载文件
 * @param onUploadProgress 上传进度
 */
export function callFileBase64String(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return httpClient.uploadFile<string>(
    {
      url: Api.CallFileBase64String,
      onUploadProgress,
    },
    params,
  );
}

/**
 * 获取LOGO
 * @returns LOGO图片对象
 */
export function getLogo() {
  return httpClient.get({
    url: PreferenceEnum.GetLogo,
  });
}

/**
 * 获取 LOGO Base64字符串
 * @returns LOGO Base64字符串
 */
export function getLogoBase64String() {
  return httpClient.get({
    url: Api.GetLogoBase64String,
  });
}

/**
 * LOGO上传
 * @param params 部分/表单数据：上载文件（multipart/form-data: upload file）
 * @param onUploadProgress 上载进度回调
 * @returns Ajax操作结果
 */
export function uploadLogo(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return httpClient.uploadFile<AjaxResult>(
    {
      url: Api.UploadLogo,
      onUploadProgress,
    },
    params,
  );
}

/**
 * LOGO删除
 * @returns Ajax操作结果
 */
export function deleteLogo() {
  return httpClient.post<AjaxResult>({
    url: Api.DeleteLogo,
  });
}
